require "Net/send_Msg"
require "Net/TitleHandler"
require 'View/UIBase/UIBase'

UISupSelect = UIBase.New(PanelNames.UISupSelect);
function UISupSelect.Init(func, _fightType,_formationType,index)
    UISupSelect.fightType = _fightType;
    UISupSelect.formationType = _formationType;
    UISupSelect.skillType = index

    UIBase.Init(UISupSelect, func);
end
local skillId = 0;
local petSkillId = 0
local supSkillInfos = {};
local supSkillData = {};
local supSkillConfig = {};
local rendererCount = 1;
--定义控件
function UISupSelect:OnCreate()
    self.backBtn = self:GetChild("bgclose");
    self.closeBtn = self:GetChild("BtnClose");
    self.list = self:GetChild("SkillList");
    self.typeCtrl = self:GetController("Type")
    self.supTab = self:GetChild("Tab_Button1")
    self.petTab = self:GetChild("Tab_Button2")
    supSkillConfig = logicMgr.SkillManager.getSupSkillConfig();
    self:addEvent();
    self:RegisterEvent(Config.EventType.AssistantSkill, self.SetInfo);
end
function UISupSelect:RemoveTimer()
end

function UISupSelect:Refresh()
    skillHandler.SendReqAssistantSkill()
    self:TimeCall(skillHandler.SendReqAssistantSkill,1000,-1)
end

function UISupSelect:OnEnable()
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UISupSelect, Config.FuncOpenType.PetPartAttr, self.petTab.name, self.petTab);
    if self.skillType then
        self.typeCtrl.selectedIndex = self.skillType
    else
        self.typeCtrl.selectedIndex = 0
    end
    if self.skillType ==0 then
        self:Refresh();
    else
        self:SetPetInfo()
    end
    self:setSkillIcon()
    self:setPetIcon()
end

function UISupSelect:OnDisable()
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UISupSelect);
end

function UISupSelect:OnDestroy()
    self:UnRegisterEvent(Config.EventType.AssistantSkill, self.SetInfo, self);
    self:RemoveTimeCall(skillHandler.SendReqAssistantSkill)
end

function UISupSelect:addEvent()
    self:RegisterOnClick(self.supTab,function()
        if FuncOpenManager.isOpen(Config.FuncOpenType.SupSkill, true, self.supTab) then
            self:SetInfo()
        end
    end)
    self:RegisterOnClick(self.petTab,function()
        if FuncOpenManager.isOpen(Config.FuncOpenType.PetPartAttr, true, self.petTab) then
            self:SetPetInfo()
        end
    end)
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.closeBtn, self.onCloseClick);
end

function UISupSelect:SetPetInfo()
    self:SetDynamicList(self.list,self.RenderPetSkillItem)
    logicMgr.PetManager.PetPartData()
    self.list.numItems = table.getn(logicMgr.PetManager.petPartSkillByGroup)
end

function UISupSelect:removeEvent()

end

function UISupSelect:onCloseClick()
    if skillId ~= 0 or petSkillId ~= 0 then
        if (self.fightType == EnumConst.FightTypeConst.RankBattle and logicMgr.RankBattleManager.BanPick()) then
            FireEvent(Config.EventType.BPReFresh_SupSkill,{skillId,petSkillId})
        else
            if self.formationType == EnumConst.FormationType.FormationEditor then 
                local index = FormationManager.GetFocus()
                FormationManager.SetTempSupskill(index, skillId)
                FormationManager.SetTempPetskill(index, petSkillId)
                FireEvent(Config.EventType.AssistantSkill)
                -- skillHandler.SendReqSelectAssistantSkillMessage(skillId,self.formationType + index)
            else
                if skillId ~=0 then
                    skillHandler.SendReqSelectAssistantSkillMessage(skillId,self.formationType)
                end
                if petSkillId and petSkillId ~= 0 then
                    skillHandler.SendReqChangePetSkill(petSkillId,self.formationType)
                end
            end
        end
        -- AudioManager.Play(2013)
    end
    skillId = 0
    petSkillId = 0
    closeUI(PanelNames.UISupSelect);
end

function UISupSelect:SetInfo()
    self:RemoveTimeCall(skillHandler.SendReqAssistantSkill)
    if self.fightType == EnumConst.FightTypeConst.RankBattle then
        supSkillInfos = logicMgr.SkillManager.getSupFullLevelSkillInfo();
    else
        supSkillInfos = logicMgr.SkillManager.getSupSkillInfo();
    end
    rendererCount = 1;
    self.list.itemRenderer = self.RenderSelectSkills;
    self.list.numItems = #supSkillConfig;
    --HelperFunc.ListTranstionPlay(self.list)
end

function UISupSelect:RenderPetSkillItem(idx,obj)
    local head = obj:GetChild("Head");
    local icon = head:GetChild("Mask"):GetChild("Icon");
    local lv = head:GetChild("number");
    local lockController = obj:GetController("lock");
    local lockText = obj:GetChild("lockText");
    local name = obj:GetChild("SkillName");
    local tip = obj:GetChild("SkillTips");
    local clickObj = obj:GetChild("clickBtn")
    obj.selected = false
    clickObj.onClick:Clear()
    local id = logicMgr.PetManager.petPartSkillByGroup[idx].f_Id;
    local cfg = logicMgr.PetManager.petPartTBSkill[tostring(id)]
    if logicMgr.SkillManager.getPetSkillIdByFormationType(self.formationType) == tonumber(cfg.f_PetSkill) then
        obj.selected = true;
    else
        obj.selected = false
    end
    if logicMgr.PetManager.petPartAllLv >= cfg.f_PetAttributeTotalLevel then
        lockController.selectedIndex = 0
        self:RegisterOnClick(clickObj,function()
            self:savePetSkillId(cfg.f_PetSkill)
        end)
    else
        lockController.selectedIndex = 1
        lockText.text = string.format(getLanguage("PetTips_10003"),cfg.f_PetAttributeTotalLevel)
    end
    lv.text = math.ceil(cfg.f_Id / 6)
    local skillData = G.dataTable["t_skillshow"][tostring(cfg.f_PetSkill)]
    icon.url = "ui://SkillAtlas/" .. skillData.f_SkillIcon
    name.text = getLanguage(skillData.f_SkillName)
    tip.text = getLanguage(skillData.f_Desc)
end

function UISupSelect:RenderSelectSkills(obj)
    local stageConfig = G.dataTable["t_stage"];
    local data = {};
    data.id = 0;
    local cfg = supSkillConfig[rendererCount];
    local head = obj:GetChild("Head");
    local icon = head:GetChild("Mask"):GetChild("Icon");
    local lv = head:GetChild("number");
    local lockController = obj:GetController("lock");
    local lockText = obj:GetChild("lockText");
    local name = obj:GetChild("SkillName");
    local tip = obj:GetChild("SkillTips");
    local clickObj = obj:GetChild("clickBtn")
    obj.selected = false
    for i = 1, #supSkillInfos do
        for j = 1, #cfg do
            if supSkillInfos[i].skillId == cfg[j].f_AssistantSkillId then
                data.id = supSkillInfos[i].skillId;
                data.oldId = supSkillInfos[i].skillOldId;
                data.data = cfg[j];
                data.info = supSkillInfos[i];
                data.skillLev = supSkillInfos[i].skillLev
            end
        end
    end
    clickObj.onClick:Clear()
    if data.info ~= nil then
        lockController.selectedIndex = 0;
        if  UISupSelect.fightType ~= EnumConst.FightTypeConst.RankBattle then
            if data.info.skillId == SkillManager.getSupSkillIdByFormationType(UISupSelect.formationType)then
                obj.selected = true;
                --skillId = data.oldId or data.id;
            else
                obj.selected = false
            end
            lv.text = SkillManager.supSkillLv;
        else
            if tonumber(data.oldId) == tonumber(logicMgr.RankBattleManager.GetSupSkill()) then
                obj.selected = true
            else
                obj.selected = false
            end
            lv.text = data.skillLev
        end
        UISupSelect:RegisterOnClick(clickObj,
                function()
                    UISupSelect:saveSkillId(data.oldId or data.id);
                end);
    else
        data.id = cfg[SkillManager.supSkillLv].f_AssistantSkillId;
        data.data = cfg[SkillManager.supSkillLv];
        lockController.selectedIndex = 1;
        lv.text = SkillManager.supSkillLv;
        local mapCfg = stageConfig[tostring(data.data.f_GetCondition[1][2])];
        lockText.text = string.format(getLanguage("TaskCondition2"),mapCfg.f_ChapterId .. "-" .. mapCfg.f_StageNum);
    end
    local skillShow = G.dataTable["t_skillshow"][tostring(data.id)];
    icon.url = "ui://SkillAtlas/" .. skillShow.f_SkillIcon;
    local skillData = G.dataTable["t_skill"][tostring(data.id)];
    name.text = getLanguage(skillData.f_SkillName);
    tip.text = getLanguage(skillData.f_Desc);
    table.insert(supSkillData, data)
    rendererCount = rendererCount + 1;
end

function UISupSelect:setSkillIcon()
    local supId = (skillId ~= 0) and skillId or SkillManager.getSupSkillIdByFormationType(UISupSelect.formationType)
    local skillData = G.dataTable["t_skillshow"][tostring(supId)]
    if skillData then
        self.supTab:GetChild("icon"):GetChild("icon").url = "ui://SkillAtlas/" .. skillData.f_SkillIcon
    end
end

function UISupSelect:setPetIcon()
    local supId = (petSkillId ~= 0) and petSkillId or logicMgr.SkillManager.getPetSkillIdByFormationType(self.formationType)
    local skillData = G.dataTable["t_skillshow"][tostring(supId)]
    if skillData then
        self.petTab:GetChild("icon"):GetChild("icon").url = "ui://SkillAtlas/" .. skillData.f_SkillIcon
    end
end

function UISupSelect:saveSkillId(id)
    skillId = id;
    self:setSkillIcon()
end

function UISupSelect:savePetSkillId(id)
    petSkillId = id;
    self:setPetIcon()
end

return UISupSelect;